<?php
namespace Library\Controller;

abstract class Controller implements iController
{
	private $Layout = "default";
	private $responseHeader = "text/html";
	private $script = array();
	private $style = array();
	
	private $dataView = array(	'siteName' => "Exo-MVC",
								'pageTitle' => "Index");
	private $dataMod = array();
	
	public function __construct()
	{
	}
	
	protected function setLayout($fileName)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Controller_Controller)
			var_dump("namespace Library\Controller; abstract class Controller -> protected function setLayout($fileName)");
		if (!empty($fileName) && file_exists(APP_ROOT."Views/Layouts/$fileName.phtml"))
		{
			$this->Layout = $fileName;
			return true;
		}
		return false;
	}
	
	protected function getLayout()
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Controller_Controller)
			var_dump("namespace Library\Controller; abstract class Controller -> protected function getLayout()");
		return $this->Layout;
	}
	
	protected function setResponseHeader($value)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Controller_Controller)
			var_dump("namespace Library\Controller; abstract class Controller -> protected function setResponseHeader($value)");
		$possibilities = array(	"txt" 	=> "text/plain",
								"html" 	=> "text/html",
								"css" 	=> "text/css",
								"js" 	=> "application/javascript",
								"json" 	=> "application/json",
								"xml" 	=> "application/xml");
		if (array_key_exists(strtolower($value), $possibilities))
		{
			$this->responseHeader = $possibilities[$value];
			return true;
		}
		return false;
	}
	
	protected function getResponseHeader()
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Controller_Controller)
			var_dump("namespace Library\Controller; abstract class Controller -> protected function getResponseHeader()");
		return $this->responseHeader;
	}
	
	protected function addScript($scriptName)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Controller_Controller)
			var_dump("namespace Library\Controller; abstract class Controller -> protected function addScript($scriptName)");
		if (is_string($scriptName) && !empty($scriptName))
		{
			array_push($this->script, $scriptName);
			return true;
		}
		return false;
	}
	
	protected function getScript($scriptName)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Controller_Controller)
			var_dump("namespace Library\Controller; abstract class Controller -> protected function getScript($scriptName)");
		return $this->script;
	}
	
	protected function addStyle($styleName)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Controller_Controller)
			var_dump("namespace Library\Controller; abstract class Controller -> protected function addStyle($styleName)");
		if (is_string($styleName) && !empty($styleName))
		{
			array_push($this->style, $styleName);
			return true;
		}
		return false;
	}
	
	protected function getStyle($styleName)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Controller_Controller)
			var_dump("namespace Library\Controller; abstract class Controller -> protected function getStyle($styleName)");
		return $this->style;
	}
	
	protected function setdataView(array $data)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Controller_Controller)
			var_dump("namespace Library\Controller; abstract class Controller -> protected function setdataView(array \$data)");
		$this->dataView = array_merge($this->dataView, $data);
	}
	
	protected function getDataView()
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Controller_Controller)
			var_dump("namespace Library\Controller; abstract class Controller -> protected function getDataView()");
		return $this->dataView;
	}
	
	protected function setDataMod($data)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Controller_Controller)
			var_dump("namespace Library\Controller; abstract class Controller -> protected function setDataMod(\$data)");
		$this->dataMod = array_merge($this->dataMod, $data);
	}
	
	protected function getDataMod()
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Controller_Controller)
			var_dump("namespace Library\Controller; abstract class Controller -> protected function getDataMod()");
		return $this->dataMod;
	}
	
	private function addFileRender(&$html)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Controller_Controller)
			var_dump("namespace Library\Controller; abstract class Controller -> private function addFileRender(&\$html)");
		foreach ($this->script as $s)
			$html = preg_replace("#</body>#", "<script src='".WEB_ROOT."js/$s.js'></script></body>", $html);
		foreach ($this->style as $s)
			$html = preg_replace("#</head>#", "<link href='".WEB_ROOT."css/$s.css' rel='stylesheet' type='text/css' /></head>", $html);
	}
	
	public function renderView($controller, $action)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Controller_Controller)
			var_dump("namespace Library\Controller; abstract class Controller -> public function renderView($controller, $action)");
		$router = \Library\Router\Router::getInstance();
		$pathView = APP_ROOT."Views/Controllers/".str_replace("Application\Controllers\\", "", $controller).'/'.str_replace("Action", "", $action).'.phtml';
		if (file_exists($pathView))
		{
			if (MOD_DEBUG)
				var_dump("la vue existe");
			header("Content-type: ".$this->getResponseHeader()."; charset=utf-8");
			extract($this->getDataView());
			ob_start();
			include_once($pathView);
			$viewContent = ob_get_clean();
			ob_start();
			include_once(APP_ROOT."Views/Layouts/".$this->getLayout().".phtml");
			$finalRender = ob_get_clean();
			$this->addFileRender($finalRender);
			echo $finalRender;
		}
		else if (!empty($_SERVER['HTTP_REFERER']))
		{
			if (MOD_DEBUG)
				var_dump("la vue n'existe pas (Sans MOD_DEBUG vous seriez redirigé vers la page précédente)");
			else
				header("location: ".$_SERVER['HTTP_REFERER']);
			die();
		}
		else
		{
			if (MOD_DEBUG)
				var_dump("la vue n'existe pas (Sans MOD_DEBUG vous seriez redirigé vers la page index)");
			else
				header("location: ".LINK_ROOT);
			die();
		}
	}
	
	public function renderModule($module, $action)
	{
		if (MOD_TRACE && MOD_TRACE_CLASS_Library_Controller_Controller)
			var_dump("namespace Library\Controller; abstract class Controller -> public function renderModule($module, $action)");
		extract($this->getDataMod());
		include_once(APP_ROOT."Views/Modules/".str_replace("Application\Modules\\", "", $module).DIRECTORY_SEPARATOR.str_replace("Action", "", $action).".phtml");
	}
}
?>